package com.hyt.it.ogt.ks.service.impl;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.exception.KsException;
import com.hyt.it.ogt.ks.mapper.CandidatePassMapper;
import com.hyt.it.ogt.ks.model.entity.CandidatePass;
import com.hyt.it.ogt.ks.model.vo.CandidatePassVO;
import com.hyt.it.ogt.ks.service.ICandidatePassService;

/**
 * <p>
 * 跳过考试流程 服务实现类
 * </p>
 *
 * @author chengxh
 * @since 2022-09-23
 */
@Service
public class CandidatePassServiceImpl extends ServiceImpl<CandidatePassMapper, CandidatePass> implements ICandidatePassService {

    @Override
    public List<String> getCandidatePassStepNames(String examId, String candidateId) {

        List<CandidatePass> candidatePassList = this.getCandidatePassSteps(examId, candidateId);
        if (candidatePassList == null || CollectionUtils.isEmpty(candidatePassList) ) {
            return new ArrayList<>();
        }

        List<String> list = new ArrayList<>();
        candidatePassList.stream().forEach(candidatePass -> {
            list.add(candidatePass.getPassSteps());
        });

        return list;
    }

    @Override
    public List<CandidatePass> getCandidatePassSteps(String examId, String candidateId) {

        List<CandidatePass> candidatePassList = this.lambdaQuery()
                .eq(StringUtils.isNoneBlank(examId),CandidatePass::getExamId,examId)
                .eq(CandidatePass::getCandidateId,candidateId)
                .eq(CandidatePass::getDelFlag,false)
                .select(CandidatePass::getPassSteps,CandidatePass::getOptionDate)
                .list();

        if (candidatePassList == null || CollectionUtils.isEmpty(candidatePassList)) {
            return new ArrayList<CandidatePass>();
        }
        return candidatePassList;
    }

    @Override
    public List<CandidatePass> getCandidatePassStepsByCandidateId(String candidateId) {
        return this.getCandidatePassSteps(null, candidateId);
    }

    @Override
    public List<CandidatePassVO> getCandidatePassStepsByExamId(String examId, String passStep) {

        List<CandidatePass> candidatePassList = this.lambdaQuery()
                .eq(CandidatePass::getExamId,examId)
                .eq(StringUtils.isNoneBlank(passStep),CandidatePass::getPassSteps,passStep)
                .eq(CandidatePass::getDelFlag,false)
                .select(CandidatePass::getCandidateId,CandidatePass::getPassSteps,CandidatePass::getOptionDate)
                .list();

        if (candidatePassList == null || CollectionUtils.isEmpty(candidatePassList)) {
            return new ArrayList<CandidatePassVO>();
        }

        List<CandidatePassVO> candidatePassVOS = new ArrayList<>();
        candidatePassList.stream().forEach(candidatePass -> {
            CandidatePassVO candidatePassVO = new CandidatePassVO();
            BeanUtils.copyProperties(candidatePass,candidatePassVO);
            candidatePassVOS.add(candidatePassVO);
        });


        return candidatePassVOS;
    }

    @Override
    public void setCandidatePassSteps(String examId, String candidateId, String stepName, String userId, String userName) {

        //1.查询是否已经设置过，则不允许重复设置
        Integer count = this.lambdaQuery()
                .eq(CandidatePass::getExamId,examId)
                .eq(CandidatePass::getCandidateId,candidateId)
                .eq(CandidatePass::getPassSteps,stepName)
                .eq(CandidatePass::getDelFlag,false)
                .count();

        if (count > 0) {
            throw new KsException(BizStatusCode.PASS_STEP_SET_EXIST.getCode(),BizStatusCode.PASS_STEP_SET_EXIST.getMsg());
        }

        CandidatePass candidatePass = new CandidatePass();
        candidatePass.setCandidateId(candidateId);
        candidatePass.setPassSteps(stepName);
        candidatePass.setExamId(examId);
        candidatePass.setUserId(userId);
        candidatePass.setUserName(userName);
        candidatePass.setOptionDate(LocalDateTime.now());
        candidatePass.setDelFlag(false);
        candidatePass.setId(UUIDUtils.randomUUID());

        this.save(candidatePass);
    }

}
